//
//  AddBankCardViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/6/29.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "AddBankCardViewController.h"
#import "AddCardTableViewCell.h"
#import "AddBankCardModel.h"
#import "MoneyCardViewController.h"

@interface AddBankCardViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, strong)UITableView * tableView;

@property (nonatomic, weak)UIButton * nextButton;

@property (nonatomic, strong)NSArray<AddBankCardModel *> * bankCardList;

@end

@implementation AddBankCardViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [AddBankCardModel bankInfoStringWithSucess:^(NSArray<AddBankCardModel *> *array) {
        
        self.bankCardList = array;
    } error:^{
        NSLog(@"bankCardList error");
    }];
}

- (void)setBankCardList:(NSArray<AddBankCardModel *> *)bankCardList{
    
    _bankCardList = bankCardList;
    
    [self.tableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    [self setupNavBar];
    [self setupUI];
}


- (void)setupNavBar{
    
    self.title = @"添加银行卡";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT - 130 * SCREENH6)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.bounces = NO;
    
    [self.tableView registerClass:[AddCardTableViewCell class] forCellReuseIdentifier:@"AddCardCell"];
    
    [self.view addSubview:self.tableView];
    
    UIButton * nextButton = [[UIButton alloc] init];
    
    [nextButton setTitle:@"添加银行卡" forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17 * SCREENH6];//
    [nextButton setBackgroundImage:[UIImage imageNamed:@"注册"] forState:UIControlStateNormal];
    nextButton.layer.borderColor = [[UIColor blackColor] CGColor];
    //边框宽度
    nextButton.layer.borderWidth =1.0f;
    //处理圆角
    nextButton.layer.cornerRadius =5.0f;
    [nextButton setTitleColor:ColorWithRGB(0xffffff) forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(didNextButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.nextButton = nextButton;
    
    [self.view addSubview:self.nextButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-20 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(46 * SCREENH6);
    }];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.bankCardList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AddCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddCardCell" forIndexPath:indexPath];
    
    //model
    AddBankCardModel *model = self.bankCardList[indexPath.item];
    cell.tag = indexPath.item;
    cell.model = model;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 65 * SCREENH6;
}



//点击添加银行卡按钮
- (void)didNextButton{
    
    MoneyCardViewController * bankCardVC = [MoneyCardViewController new];
    
    [self.navigationController pushViewController:bankCardVC animated:YES];

}



- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
